package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.bumptech.glide.Glide;
import com.stark.pdf.lib.model.PdfUtil;

import java.io.File;

import flc.ast.BaseAc;
import flc.ast.HomeActivity;
import flc.ast.R;
import flc.ast.databinding.ActivityConvTextBinding;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.inf.IRetCallback;
import stark.common.basic.utils.FileP2pUtil;
import stark.common.basic.utils.IntentUtil;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.UriUtil;

public class ConvTextActivity extends BaseAc<ActivityConvTextBinding> {

    public static String content;
    private String savePath;

    @Override
    protected int onCreate() {
        return R.layout.activity_conv_text;
    }

    @Override
    protected void initView() {
        mDataBinding.ivConvTextBack.setOnClickListener(v -> onBackPressed());
        mDataBinding.ivSave.setOnClickListener(this);
        mDataBinding.ivShare.setOnClickListener(this);
        mDataBinding.ivDownload.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivShare://分享
                shareFile();
                break;
            case R.id.ivDownload://下载
                download();
                break;
            case R.id.ivSave://保存
                saveToRecord();
                break;
        }
    }

    @Override
    protected void initData() {
        createPdf();
    }

    //下载
    private void download() {
        showDialog(getString(R.string.save_ing));
        RxUtil.create(new RxUtil.Callback<String>() {
            @Override
            public void doBackground(ObservableEmitter<String> emitter) {
                FileP2pUtil.copyPrivateVideoToPublic(mContext, savePath);
                emitter.onNext("");
            }

            @Override
            public void accept(String path) {
                dismissDialog();
                ToastUtils.showShort(R.string.download_suc);
                startActivity(HomeActivity.class);
            }
        });
    }

    //保存到转换记录
    private void saveToRecord() {
        showDialog(getString(R.string.save_ing));
        RxUtil.create(new RxUtil.Callback<String>() {
            @Override
            public void doBackground(ObservableEmitter<String> emitter) {
                String suffix = FileUtils.getFileExtension(savePath);
                String desPath = PathUtils.getFilesPathExternalFirst() + Content.myConvRecord
                        + "/" + FileUtils.getFileNameNoExtension(savePath) + "." + suffix;
                FileUtils.copy(savePath, desPath);
                emitter.onNext("");
            }

            @Override
            public void accept(String path) {
                dismissDialog();
                ToastUtils.showShort(R.string.saved_record);
                startActivity(HomeActivity.class);
            }
        });
    }

    //分享文件
    private void shareFile() {
        Uri uri = UriUtil.path2Uri(mContext, savePath);
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_SEND);
        intent.setType("application/*");
        intent.putExtra(Intent.EXTRA_STREAM, uri);
        startActivity(Intent.createChooser(intent, "Share File"));
    }

    private void createPdf() {
        showDialog(getString(R.string.conv_ing));
        PdfUtil.createPdf(content, new IRetCallback<String>() {
            @Override
            public void onResult(String result) {
                savePath = result;
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        mDataBinding.pdfView.fromFile(new File(result)).load();
                    }
                }, 1000);
                dismissDialog();
            }
        });
    }

}